package laf.startup;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.tools.rma.rma;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import jade.wrapper.StaleProxyException;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import laf.agents.CognitiveAgent;
import laf.agents.LanguageSymbolDictionary;
import laf.env.Attribute;
import laf.env.AttributeDomain;
import laf.env.AttributeValue;
import laf.env.EnvObject;
import laf.env.Enviroment;
import laf.env.State;
import laf.lang.Symbol;
import laf.learning.GroundingStrategyImpl;
import laf.learning.KESGroundingRule;
import laf.learning.ReactiveSymbolGroundingBehaviour;
import laf.time.Timer;

public class StartUp {
	private static final long TICK_DURATION = 10000;

	public static void main(String[] args) {
		
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.WARNING);
		Logger.getLogger("laf").setLevel(Level.WARNING);
		Logger.getLogger("laf.agents.CognitiveAgent.NA").setLevel(Level.INFO);
		Timer timer = Timer.getInstance();
		timer.start(TICK_DURATION);
		Enviroment env = Enviroment.getInstance();
		List<Symbol> symbols = new ArrayList<Symbol>();
		
		AttributeDomain domain1 = new AttributeDomain();
		for (int i = 0; i < 10; i++) {
			domain1.addValue(new AttributeValue(i));
			symbols.add(new Symbol("velocity"+i));
			symbols.add(new Symbol("position"+i));
		}		
		env.put("domain1", domain1);
		
		Attribute velocity = new Attribute("velocity", domain1);
		Attribute position = new Attribute("position", domain1);
		env.put("velocity", velocity);
		env.put("position", position);
		
		EnvObject object;
		for (int i = 0; i < 4; i++) {
			State state = new State();
			state.putValue(velocity.getValues().iterator().next());
			state.putValue(velocity.getValues().iterator().next());
			object = new EnvObject("o" + i, state);
			env.put("o" + i, object);
			object.start();
		}
		
		LanguageSymbolDictionary dictionary = new LanguageSymbolDictionary(symbols, env.getAttributes());
		int i = 0, j = 0;
		for (Symbol symbol : symbols) {
			j = 0;
			for (AttributeValue	attrValue : velocity.getValues()) {
				dictionary.setCredibility(symbol, attrValue, Long.MAX_VALUE);
				if (i == j)
					dictionary.setCorrelation(symbol, attrValue, 1.0);
				++j;
			}
			++i;
		}
		
		LanguageSymbolDictionary copy = new LanguageSymbolDictionary(dictionary);
		
		Runtime jadeRT = Runtime.instance();
		Profile profile = new ProfileImpl();
		ContainerController containerCntr = jadeRT.createMainContainer(profile);
		try {
			Object[] arguments = { copy };
			AgentController agentCntr = containerCntr.createNewAgent("CA1", CognitiveAgent.class.getName(), arguments);
			agentCntr.start();
			
			CognitiveAgent newAgent = new CognitiveAgent();
			newAgent.addBehaviour(new ReactiveSymbolGroundingBehaviour(newAgent, new GroundingStrategyImpl(new KESGroundingRule())));
			agentCntr = containerCntr.acceptNewAgent("NA", newAgent);
			agentCntr.start();
			
			agentCntr = containerCntr.createNewAgent("RMA", rma.class.getName(), null);
			agentCntr.start();
		} catch (StaleProxyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(dictionary);
	}
}
